Slient Blog

自定义View之网状分布图

2017-03-26


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
public class MyViewFive extends View {
private static final String TAG = "MyViewFive";
private int count = 6;
private float angle = (float) (Math.PI * 2 / count);
private float radius;//网格最大半径
private int centerX;//中心x
private int centerY;//中心y
private String[] titles = {"a", "b", "c", "d", "e", "f"};
private double[] data = {100, 60, 90, 30, 100, 50, 10, 20}; //各维度分值
private float maxValue = 100;//数据最大值
private Paint mainPaint;//雷达区画笔
private Paint valuePaint;//数据区画笔
private Paint textPaint;//文本画笔
public MyViewFive(Context context) {
super(context);
init();
}
public MyViewFive(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
radius = Math.min(w, h) / 2 * 0.9f;
centerX = w / 2;
centerY = h / 2;
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPolygon(canvas);
drawLines(canvas);
drawText(canvas);
drawValue(canvas);
}
private void init(){
mainPaint=new Paint();
mainPaint.setColor(Color.BLACK);
mainPaint.setStyle(Paint.Style.STROKE);
mainPaint.setStrokeWidth(3f);
textPaint=new Paint();
textPaint.setColor(Color.BLUE);
textPaint.setTextSize(50f);
valuePaint=new Paint();
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
valuePaint.setColor(Color.BLUE);
}
private void drawPolygon(Canvas canvas){
Path path = new Path();
float r = radius / (count - 1);//r是蜘蛛丝之间的间距
for (int i = 1; i < count; i++) {//中心点不用绘制
float curR = r * i;//当前半径
path.reset();
for (int j = 0; j < count; j++) {
if (j == 0) {
path.moveTo(centerX + curR, centerY);
} else {
//根据半径,计算出蜘蛛丝上每个点的坐标
float x = (float) (centerX + curR * Math.cos(angle * j));
float y = (float) (centerY + curR * Math.sin(angle * j));
path.lineTo(x, y);
}
}
path.close();//闭合路径
canvas.drawPath(path, mainPaint);
}
}
private void drawLines(Canvas canvas){
Path path=new Path();
for (int i = 0; i < count; i++) {
path.reset();
path.moveTo(centerX,centerY);
float x= (float) (centerX+radius*Math.cos(angle*i));
float y= (float) (centerY+radius*Math.sin(angle*i));
path.lineTo(x,y);
canvas.drawPath(path,mainPaint);
}
}
private void drawText(Canvas canvas) {
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
float fontHeight = fontMetrics.descent - fontMetrics.ascent;
for (int i = 0; i < count; i++) {
float x = (float) (centerX + (radius + fontHeight / 2) * Math.cos(angle * i));
float y = (float) (centerY + (radius + fontHeight / 2) * Math.sin(angle * i));
canvas.drawText(titles[i], x , y, textPaint);
}
}
private void drawValue(Canvas canvas){
Path path=new Path();
valuePaint.setAlpha(245);
for (int i = 0; i < count; i++) {
double percentage=data[i]/maxValue;
float x= (float) (centerX+radius*Math.cos(angle*i)*percentage);
float y= (float) (centerY+radius*Math.sin(angle*i)*percentage);
if (i==0){
path.moveTo(x,y);
}else {
path.lineTo(x,y);
}
canvas.drawCircle(x,y,10,valuePaint);
}
valuePaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,valuePaint);
valuePaint.setAlpha(170);
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawPath(path,valuePaint);
}
}
public class MyViewFive extends View {
private static final String TAG = "MyViewFive";
private int count = 6;
private float angle = (float) (Math.PI * 2 / count);
private float radius;//网格最大半径
private int centerX;//中心x
private int centerY;//中心y
private String[] titles = {"a", "b", "c", "d", "e", "f"};
private double[] data = {100, 60, 90, 30, 100, 50, 10, 20}; //各维度分值
private float maxValue = 100;//数据最大值
private Paint mainPaint;//雷达区画笔
private Paint valuePaint;//数据区画笔
private Paint textPaint;//文本画笔
public MyViewFive(Context context) {
super(context);
init();
}
public MyViewFive(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
radius = Math.min(w, h) / 2 * 0.9f;
centerX = w / 2;
centerY = h / 2;
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPolygon(canvas);
drawLines(canvas);
drawText(canvas);
drawValue(canvas);
}
private void init(){
mainPaint=new Paint();
mainPaint.setColor(Color.BLACK);
mainPaint.setStyle(Paint.Style.STROKE);
mainPaint.setStrokeWidth(3f);
textPaint=new Paint();
textPaint.setColor(Color.BLUE);
textPaint.setTextSize(50f);
valuePaint=new Paint();
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
valuePaint.setColor(Color.BLUE);
}
private void drawPolygon(Canvas canvas){
Path path = new Path();
float r = radius / (count - 1);//r是蜘蛛丝之间的间距
for (int i = 1; i < count; i++) {//中心点不用绘制
float curR = r * i;//当前半径
path.reset();
for (int j = 0; j < count; j++) {
if (j == 0) {
path.moveTo(centerX + curR, centerY);
} else {
//根据半径,计算出蜘蛛丝上每个点的坐标
float x = (float) (centerX + curR * Math.cos(angle * j));
float y = (float) (centerY + curR * Math.sin(angle * j));
path.lineTo(x, y);
}
}
path.close();//闭合路径
canvas.drawPath(path, mainPaint);
}
}
private void drawLines(Canvas canvas){
Path path=new Path();
for (int i = 0; i < count; i++) {
path.reset();
path.moveTo(centerX,centerY);
float x= (float) (centerX+radius*Math.cos(angle*i));
float y= (float) (centerY+radius*Math.sin(angle*i));
path.lineTo(x,y);
canvas.drawPath(path,mainPaint);
}
}
private void drawText(Canvas canvas) {
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
float fontHeight = fontMetrics.descent - fontMetrics.ascent;
for (int i = 0; i < count; i++) {
float x = (float) (centerX + (radius + fontHeight / 2) * Math.cos(angle * i));
float y = (float) (centerY + (radius + fontHeight / 2) * Math.sin(angle * i));
canvas.drawText(titles[i], x , y, textPaint);
}
}
private void drawValue(Canvas canvas){
Path path=new Path();
valuePaint.setAlpha(245);
for (int i = 0; i < count; i++) {
double percentage=data[i]/maxValue;
float x= (float) (centerX+radius*Math.cos(angle*i)*percentage);
float y= (float) (centerY+radius*Math.sin(angle*i)*percentage);
if (i==0){
path.moveTo(x,y);
}else {
path.lineTo(x,y);
}
canvas.drawCircle(x,y,10,valuePaint);
}
valuePaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,valuePaint);
valuePaint.setAlpha(170);
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawPath(path,valuePaint);
}
}

使用微信添加

若你觉得我的文章对你有帮助,请添加我为好友

扫描二维码,分享此文章